/* $RCSFile: ADNDynPhoneCall.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/CRMWrapper3/DynEntities/ADNDynPhoneCall.cs $
 * $log$
 * Revision 6 2010/08/06 19:56:33  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Diagnostics;
using System.Collections;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text;
using CrmSdk;

namespace AddonNice.CRMWrapper
{
	public class ADNDynPhoneCall : ADNDynActivity
	{
        protected class Ftor: DFactor
        {
            public Ftor() : base(CrmStr.phonecall)
            {}

            public override Type DynType
            { get { return typeof(ADNDynPhoneCall); } }

            public override string DynTypename
            {   get{ return CrmStr.phonecall;}}

            public override DynEntity Build()
            {   return new ADNDynPhoneCall();}

            public override DynEntity Build(CRMWrapperBase w, InternalDynamicEntity dEntity, PropertyDescriptorCollection wproperties)
            {   return new ADNDynPhoneCall(w, dEntity, wproperties, null);}

            public override DynEntity Build(CRMWrapperBase w, string PrimaryFieldValue)
            {   return new ADNDynPhoneCall(w,PrimaryFieldValue);}

            public override DynEntity Build(CRMWrapperBase w,Guid PrimaryKeyValue)
            {   return new ADNDynPhoneCall(w,PrimaryKeyValue);}
        }

        static ADNDynPhoneCall(){new Ftor();} 

        /// <summary>
		/// Reserved for template methods: do not use directly
		/// </summary>
        public ADNDynPhoneCall(): base()
		{}

        public ADNDynPhoneCall(CRMWrapperBase w,string subj,bool ForCreate)
			: base(w,subj,CrmStr.phonecall)
		{}

        public ADNDynPhoneCall(CRMWrapperBase w,string subj)
			: base(w, subj,CrmStr.phonecall)
		{}

        public ADNDynPhoneCall(CRMWrapperBase w,string subj,string body,DynEntity sender,DynEntity dest)
			: base(w, subj,CrmStr.phonecall)
		{
            description =   body;
            AddSender(sender);
            AddDest(dest);
		}

        public ADNDynPhoneCall(CRMWrapperBase w, Guid CrmId)
			: base(w,CrmStr.phonecall, CrmId)
		{}

		public ADNDynPhoneCall(CRMWrapperBase w, InternalDynamicEntity e)
			: base(w,CrmStr.phonecall,e,null,null)
		{}

		public ADNDynPhoneCall(CRMWrapperBase w, InternalDynamicEntity e,PropertyDescriptorCollection prop,InternalColumnSet s)
			: base(w,CrmStr.phonecall,e,prop,s)
		{}

        public ADNDynPhoneCall(ResetWrapperHandler fn,string subj)
			: base(fn,subj,CrmStr.phonecall)
		{}

        public ADNDynPhoneCall(ResetWrapperHandler fn,string subj,string body,DynEntity sender,DynEntity dest)
			: base(fn, subj,CrmStr.phonecall)
		{
            description =   body;
            AddSender(sender);
            AddDest(dest);
		}

        public ADNDynPhoneCall(ResetWrapperHandler fn, Guid CrmId)
			: base(fn,CrmStr.phonecall, CrmId)
		{}

		public ADNDynPhoneCall(ResetWrapperHandler fn, InternalDynamicEntity e)
			: base(fn,CrmStr.phonecall,e,null,null)
		{}

		public ADNDynPhoneCall(ResetWrapperHandler fn, InternalDynamicEntity e,PropertyDescriptorCollection prop,InternalColumnSet s)
			: base(fn,CrmStr.phonecall,e,prop,s)
		{}

        /// <summary>
        /// Gets or sets a value that indicates whether the message came in to Microsoft Dynamics CRM or was sent out.
        /// true for an outgoing message or false for an ingoing message.
        /// </summary>
        [CrmDynAttribute(CrmStr.phonecall)]
        public bool directioncode
        {
            get
            {
                return BooleanPropertyValue(CrmStr.directioncode);
            }
            set
            {
                AddBooleanProperty(CrmStr.directioncode,value);
            }
        }

        /*
        bcc Gets or sets the blind carbon copy (BCC) recipients. 
        cc Gets or sets the carbon copy (CC) recipients. 
        from Gets or sets the information about who sent the e-mail. 
        to Gets or sets the recipient party list for the e-mail, and references recipient records such as users and queues. 
        */
        public List<ADNDynActivityParty> fromList    =   new List<ADNDynActivityParty>();
        public List<ADNDynActivityParty> toList      =   new List<ADNDynActivityParty>();

        public void AddSender(DynEntity ent)
        {
            ADNDynActivityParty ap   =   new ADNDynActivityParty(wrapper);
            ap.DefaultPartyName     =   ent.SchemaName;
            ap.partyid              =   ent.PrimaryKeyGuidValue;
            fromList.Add(ap);
        }

        public void AddDest(DynEntity ent)
        {
            ADNDynActivityParty ap   =   new ADNDynActivityParty(wrapper);
            ap.DefaultPartyName     =   ent.SchemaName;
            ap.partyid              =   ent.PrimaryKeyGuidValue;
            toList.Add(ap);
        }

        [CrmDynAttribute(CrmStr.phonecall)]
        public InternalDynamicEntityArray from
        {
            get
            {
                return DynamicEntityArrayProperty(CrmStr.from);
            }
            set
            {
                AddDynamicEntityArrayProperty(CrmStr.from,value);
            }
        }

        [CrmDynAttribute(CrmStr.phonecall)]
        public InternalDynamicEntityArray to
        {
            get
            {
                return DynamicEntityArrayProperty(CrmStr.to);
            }
            set
            {
                AddDynamicEntityArrayProperty(CrmStr.to,value);
            }
        }

        /// <summary>
        /// Owner for this object
        /// </summary>
        [CrmDynAttribute(CrmStr.phonecall)]
        public override Guid ownerid
        {
            get
            {
                if ( ContainsKey(CrmStr.ownerid) )
                    return OwnerPropertyValue(CrmStr.ownerid);
                return Guid.Empty;
            }
            set
            {
                AddOwnerProperty(CrmStr.ownerid,CrmStr.systemuser,value);
            }
        }

        protected override Guid internalCreate(bool isAdmin)
        {
            if ( ( fromList.Count == 0 ) && !dicProperty.ContainsKey(CrmStr.from))
            {
                const string msg    =   "[E]ADNDynPhoneCall internalCreate 'From' list empty";
                if ( CRMDynTraceSwitch.Sw.TraceError)
                    Trace.WriteLine( msg,CRMDynTraceSwitch.Sw.Info);
                //throw new Exception(msg);
            }
            if ((toList.Count == 0) && !dicProperty.ContainsKey(CrmStr.to))
            {
                const string msg    =   "[E]ADNDynPhoneCall internalCreate 'To' list empty";
                if ( CRMDynTraceSwitch.Sw.TraceError)
                    Trace.WriteLine( msg,CRMDynTraceSwitch.Sw.Info);
                //throw new Exception(msg);
            }
            if (fromList.Count > 0)
            {
                InternalDynamicEntityArray newArray = wrapper.Util.MakeDynamicArray(fromList);
                AddDynamicEntityArrayProperty(CrmStr.from, newArray);
            }

            if (toList.Count > 0)
            {
                InternalDynamicEntityArray newArray = wrapper.Util.MakeDynamicArray(toList);
                AddDynamicEntityArrayProperty(CrmStr.to, newArray);
            }
            return wrapper.CreateFromDynEntity(this,isAdmin);
        }
    }
}
